package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.FeeList;
import com.edward.model.SelfHarvest;


public class FeeListManager extends HibernateDaoSupport {
	
	public void addFeeList(FeeList feeList) {
		
		getHibernateTemplate().save(feeList);
	}
	
	@SuppressWarnings("unchecked")
	public void updateFeeList(FeeList feeList) {
		getHibernateTemplate().update(feeList);
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(feeList.getDate());
		String m = mm.format(feeList.getDate());
		List<FeeList> l = getHibernateTemplate().find("from FeeList f where year(f.date) = '" + y + "' and month(f.date) = '" + m + "'and f.flag = 't'"  );
		double sum = 0;
		for(FeeList ff : l){
			sum += ff.getSum();
		}
		addFeeHarvest(sum, feeList.getDate());
		
	}
	
	@SuppressWarnings("unchecked")
	public void deleteFeeList(int id){
		FeeList f = getFeeList(id);
		Date date = f.getDate();
		getHibernateTemplate().delete(getFeeList(id));
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(date);
		String m = mm.format(date);
		List<FeeList> l = getHibernateTemplate().find("from FeeList f where year(f.date) = '" + y + "' and month(f.date) = '" + m + "'and f.flag = 't'"  );
		double sum = 0;
		for(FeeList ff : l){
			sum += ff.getSum();
		}
		addFeeHarvest(sum, date);
	}
	
	public FeeList getFeeList(int id){
		FeeList d =(FeeList)getHibernateTemplate().get(FeeList.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<FeeList> listFeeList() {
		return getHibernateTemplate().find("from FeeList");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <FeeList>list = getHibernateTemplate().find("from FeeList" );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <FeeList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String query,String type) {
		String y = query.substring(0,4);
		String m = query.substring(5,7);
		String sql = "from FeeList f where f.type = '" + type + "' and year(f.date) = '" + y + "' and month(f.date) = '" + m + "'and f.flag is null order by f.date" ;
		List<FeeList> list = getHibernateTemplate().find(sql);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <FeeList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
	}
		
	

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerSumList(int start,int limit,String query,String type) {
		if(query.equals("")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			query = sdf.format(new Date());
		}
		
		List <FeeList>list = getHibernateTemplate().find("from FeeList f where f.type = '" + type + "' and year(f.date) = '" + query + "' and f.flag = 't' order by f.date");
		FeeList f = new FeeList(); 
		for (FeeList fee : list) {
			f.setDepreciation(f.getDepreciation()+fee.getDepreciation());
			f.setEduRate(f.getEduRate()+fee.getEduRate());
			f.setElecRate(f.getElecRate()+fee.getElecRate());
			f.setGather(f.getGather()+fee.getGather());
			f.setInterest(f.getInterest()+fee.getInterest());
			f.setMeal(f.getMeal()+fee.getMeal());
			f.setOfficRate(f.getOfficRate()+fee.getOfficRate());
			f.setOperationRate(f.getOperationRate()+fee.getOperationRate());
			f.setOther(f.getOther()+fee.getOther());
			f.setRent(f.getRent()+fee.getRent());
			f.setTeleRate(f.getTeleRate()+fee.getTeleRate());
			f.setTwRate(f.getTwRate()+fee.getTwRate());
			f.setWage(f.getWage()+fee.getWage());
			f.setWaterRate(f.getWaterRate()+fee.getWaterRate());
			f.setSocial(f.getSocial() + fee.getSocial());
			f.setSport(f.getSport() + fee.getSport());
			f.setSum(f.getSum()+fee.getSum());
		 }
		f.setDate(null);
		list.add(f);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <FeeList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")
	//判断有没有该月份的合计，如果没有就添加 设type 为T
	public void addcheck (FeeList f){
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(f.getDate());
		String m = mm.format(f.getDate());
		List<FeeList> list = getHibernateTemplate().find("from FeeList f where f.type = '" + f.getType() + "' and year(f.date) = '" + y + "' and month(f.date) = '" + m + "'and f.flag = 't'"  );
		if(list.size()==0){
			f.setFlag("t");
			f.setId(0);
			addFeeList(f);
		}
		else{
			FeeList fl = list.get(0);
			fl.setDepreciation(fl.getDepreciation()+f.getDepreciation());
			fl.setEduRate(fl.getEduRate()+f.getEduRate());
			fl.setElecRate(fl.getElecRate()+f.getElecRate());
			fl.setGather(fl.getGather()+f.getGather());
			fl.setInterest(fl.getInterest()+f.getInterest());
			fl.setMeal(fl.getMeal()+f.getMeal());
			fl.setOfficRate(fl.getOfficRate()+f.getOfficRate());
			fl.setOperationRate(fl.getOperationRate()+f.getOperationRate());
			fl.setOther(fl.getOther()+f.getOther());
			fl.setRent(fl.getRent()+f.getRent());
			fl.setTeleRate(fl.getTeleRate()+f.getTeleRate());
			fl.setTwRate(fl.getTwRate()+f.getTwRate());
			fl.setWage(fl.getWage()+f.getWage());
			fl.setWaterRate(fl.getWaterRate()+f.getWaterRate());
			fl.setSocial(fl.getSocial() + f.getSocial());
			fl.setSport(fl.getSport() + f.getSport());
			fl.setSum(fl.getSum()+f.getSum());
		}
		List<FeeList> l = getHibernateTemplate().find("from FeeList f where year(f.date) = '" + y + "' and month(f.date) = '" + m + "'and f.flag = 't'"  );
		double sum = 0;
		for(FeeList ff : l){
			sum += ff.getSum();
		}
		addFeeHarvest(sum, f.getDate());
	}
	
	@SuppressWarnings("unchecked")
	public FeeList getSum(Date date,String type){
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(date);
		String m = mm.format(date);
		List<FeeList> list = getHibernateTemplate().find("from FeeList f where f.type = '" + type + "' and year(f.date) = '" + y + "' and month(f.date) = '" + m + "'and f.flag = 't'"  );
		return list.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public void addFeeHarvest(double sum,Date date){
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(date);
		String m = mm.format(date);
		List <SelfHarvest> list = getHibernateTemplate().find("from SelfHarvest s where year(s.date) = '" + y + "' and month(s.date) = '" + m + "'");
		if(list.size() != 0){
			for(SelfHarvest sh : list){
				if(sh.getType() == null){
					sh.setFee(sum);
					sh.setNetProfit(sh.getIncome() - sh.getCost() - sh.getFee() - sh.getTax() + sh.getBalance() - sh.getOther() - sh.getLoss());
					if(sh.getNetProfit()==0||sh.getIncome()==0){
						sh.setRate(0);
					}else{
						sh.setRate(sh.getNetProfit()/sh.getIncome()*100);
						int i = 0;
						double d ;
						i = (int) (sh.getRate() * 1000);
						d = i;
						sh.setRate(d/1000);
					}
					getHibernateTemplate().save(sh);
				}else{
					sh.setFee(sum/5);
					sh.setNetProfit(sh.getIncome() - sh.getCost() - sh.getFee() - sh.getTax() + sh.getBalance() - sh.getOther() - sh.getLoss());
					if(sh.getNetProfit()==0||sh.getIncome()==0){
						sh.setRate(0);
					}else{
						sh.setRate(sh.getNetProfit()/sh.getIncome()*100);
						int i = 0;
						double d ;
						i = (int) (sh.getRate() * 1000);
						d = i;
						sh.setRate(d/1000);
					}
					getHibernateTemplate().save(sh);
				}
			}
			
		}else{
			SelfHarvest sh = new SelfHarvest();
			List <SelfHarvest> l = new ArrayList<SelfHarvest>();
			sh.setDate(date);
			sh.setFee(sum);
			sh.setSelfs(l);
			getHibernateTemplate().save(sh);
			for(int i=0; i<5;i++){
					SelfHarvest st = new SelfHarvest();
					st.setType(judge(i));
					st.setDate(date);
					st.setFee(sum/5);
					getHibernateTemplate().save(st);
					sh.getSelfs().add(st);
			}
			getHibernateTemplate().save(sh);
		}
		
		
		
	}
	
	public String judge (int i){
		if(i == 0){
			return "汽车";
		}else if (i == 1){
			return "摩托车";
		}else if (i == 2){
			return "汽车配件";
		}else if (i == 3){
			return "摩托车配件";
		}else{
			return "其他";
		}
	}
	


}



